package com.hitqz.robot.biz.service.impl;

import com.hitqz.robot.biz.service.AreaActionStrategy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.DataOutputStream;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

/**
 * @author xupkun
 * @date 2024/12/16
 */

@Service("agvGate")
@RequiredArgsConstructor
@Slf4j
public class AgvGateServiceImpl implements AreaActionStrategy {
    @Override
    public int doActionAnyWhere(double distance, String params) {
        return 0;
    }

    @Override
    public int doActionInArea(double distance, String params) {
        //log.info("进入区域:" + distance);
        String[] ips = params.split(",");
        for (int i = 0; i < ips.length; i++) {
            try {
                String ip = ips[i]; int port = 10000;
                log.info("doActionEnterArea发送打开"+ ip);
                byte[] close1 = new byte[] {
                        (byte) 0xFE, 0x05, 0x00,0x00, (byte) 0x00, 0x00, (byte) 0xD9, (byte) 0xC5
                };
                byte[] open2 = new byte[] {
                        (byte)0xFE, 0x05, 0x00, 0x01,  (byte)0xFF, 0x00, (byte) 0xC9, (byte) 0xF5
                };
                byte[] open3 = new byte[] {
                        (byte) 0xFE, 0x05, 0x00, 0x02, (byte) 0xFF, 0x00, 0x39, (byte) 0xF5
                };

                byte[] open4 = new byte[] {
                        (byte) 0xFE, 0x05, 0x00, 0x03, (byte) 0xFF, 0x00, 0x68, (byte) 0x35
                };

                byte[] close2 = new byte[] {
                        (byte) 0xFE, 0x05, 0x00 , 0x01 , 0x00 , 0x00 , (byte) 0x88, 0x05
                };

                byte[] close3 = new byte[] {
                        (byte) 0xFE, 0x05 , 0x00 , 0x02 , 0x00 , 0x00 , 0x78 , 0x05
                };
                byte[] close4 = new byte[] {
                        (byte) 0xFE, 0x05 , 0x00 , 0x03 , 0x00 , 0x00 , 0x29 , (byte) 0xC5
                };

                send(ip, close1);
                TimeUnit.MILLISECONDS.sleep(500);
                send(ip, open2);
                TimeUnit.MILLISECONDS.sleep(500);
                send(ip, open3);
                TimeUnit.MILLISECONDS.sleep(500);
                send(ip, open4);
                TimeUnit.MILLISECONDS.sleep(500);
                send(ip, close2);
            } catch (Exception e) {
                log.info("进入区域异常:" + e.getMessage());
            }
        }
        return 0;
    }

    @Override
    public int doActionEnterArea(double distance, String params) {
        if (distance > 3) {
            return 0;
        }



        return 0;
    }

    @Override
    public int doActionLeaveArea(double distance, String params) {
        log.info("离开区域的距离:" + distance);
        if (distance > 3) {
            return 0;
        }
        String[] ips = params.split(",");
        for (int i = 0; i < ips.length; i++) {
            try {
                String ip = ips[i]; int port = 10000;
                log.info("doActionLeaveArea发送打开" + ip);
                byte[] close2 = new byte[] {
                        (byte) 0xFE, 0x05, 0x00 , 0x01 , 0x00 , 0x00 , (byte) 0x88, 0x05
                };

                byte[] close3 = new byte[] {
                        (byte) 0xFE, 0x05 , 0x00 , 0x02 , 0x00 , 0x00 , 0x78 , 0x05
                };
                byte[] close4 = new byte[] {
                        (byte) 0xFE, 0x05 , 0x00 , 0x03 , 0x00 , 0x00 , 0x29 , (byte) 0xC5
                };
                byte[] open1 = new byte[] {
                        (byte) 0xFE, 0x05 , 0x00 , 0x00 , (byte) 0xFF, 0x00 , (byte) 0x98, 0x35
                };
                byte[] close1 = new byte[] {
                        (byte) 0xFE, 0x05, 0x00,0x00, (byte) 0x00, 0x00, (byte) 0xD9, (byte) 0xC5
                };
                TimeUnit.MILLISECONDS.sleep(500);
                send(ip, close2);
                TimeUnit.MILLISECONDS.sleep(500);
                send(ip, open1);
                TimeUnit.MILLISECONDS.sleep(500);
                send(ip, close3);
                TimeUnit.MILLISECONDS.sleep(500);
                send(ip, close4);
                TimeUnit.MILLISECONDS.sleep(500);
                send(ip, close1);
            } catch (Exception e) {
                log.info("离开区域异常:" + e.getMessage());
            }
        }
        return 0;
    }


    public static void send(String ip, byte[] data) {
        try {
            int port = 10000;
            Socket socket = new Socket(ip, port);
            socket.setSoTimeout(60000);
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            out.write(data);
            out.close();
            socket.close();
        } catch (Exception e) {

        }
    }
}
